<template>
  <div>
    <span class="el-menu__title">修改当前护工信息</span>
    <el-form ref="updateUser" :model="updateUser" label-width="80px"
             :rules="updateUserrulers" label-position="right">
      <el-form-item label="账户">
        <el-col :span="4">
          <el-input v-model="this.$store.getters.getLoginParam.username" readonly placeholder=""></el-input>
        </el-col>
      </el-form-item>
      <el-form-item label="真实姓名">
        <el-col :span="4">
          <el-input v-model="this.$store.getters.getLoginParam.realname" readonly placeholder=""></el-input>
        </el-col>
      </el-form-item>
      <el-form-item label="性别">
        <el-col :span="4">
          <el-input v-model="this.$store.getters.getLoginParam.sex" readonly placeholder=""></el-input>
        </el-col>
        <el-col :span="2" style="text-align: center">
          <span>---------</span>
        </el-col>
        <el-col :span="4">
          <el-select v-model="updateUser.sex" placeholder="" style="width: 260px">
            <el-option
              v-for="item in options"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-col>
      </el-form-item>
      <el-form-item label="住址" prop="address">
        <el-col :span="4">
          <el-input v-model="this.$store.getters.getLoginParam.address" readonly placeholder=""></el-input>
        </el-col>
        <el-col :span="2" style="text-align: center">
          <span>---------</span>
        </el-col>
        <el-col :span="4">
          <el-input v-model="updateUser.address" clearable placeholder="请输入新住址"></el-input>
        </el-col>
      </el-form-item>
      <el-form-item label="电话" prop="telephone">
        <el-col :span="4">
          <el-input v-model="this.$store.getters.getLoginParam.telephone" readonly placeholder=""></el-input>
        </el-col>
        <el-col :span="2" style="text-align: center">
          <span>---------</span>
        </el-col>
        <el-col :span="4">
          <el-input v-model="updateUser.telephone" clearable placeholder="请输入新电话"></el-input>
        </el-col>
      </el-form-item>
      <el-form-item label="年龄" prop="age">
        <el-col :span="4">
          <el-input v-model="this.$store.getters.getLoginParam.age" readonly placeholder=""></el-input>
        </el-col>
        <el-col :span="2" style="text-align: center">
          <span>---------</span>
        </el-col>
        <el-col :span="4">
          <el-input v-model="updateUser.age" clearable placeholder="请输入年龄"></el-input>
        </el-col>
      </el-form-item>
      <el-form-item label="薪资">
        <el-col :span="4">
          <el-input v-model="this.$store.getters.getLoginParam.salary" readonly placeholder=""></el-input>
        </el-col>
      </el-form-item>
      <el-form-item label="状态">
        <el-col :span="4">
          <el-input v-model="this.assStatusss" readonly placeholder=""></el-input>
        </el-col>
      </el-form-item>
      <el-form-item>
        <el-col :span="10" style="text-align: right">
          <el-button type="primary" @click="updateUsers()">修改</el-button>
        </el-col>
      </el-form-item>
    </el-form>
  </div>

</template>

<script>
    import {postRequest} from "../../utils/api";

    export default {
        name: "UpdateAssis",
        data() {
            return {
                updateUser: {
                    sex: '女',
                    address: '',
                    telephone: '',
                    age: '',
                    assStatus: ''
                },
                User: {},
                updateUserrulers: {
                    address: [{required: true, message: '住址不可置空', trigger: 'blur'}],
                    telephone: [{len: 11, required: true, message: '请正确输入电话号码（11位）', trigger: 'blur'}],
                    age: [{required: true, message: '年龄不可为空', trigger: 'blur'}],
                },
                options: [{
                    value: '男',
                    label: '男'
                }, {
                    value: '女',
                    label: '女'
                },],
                assStatusss: ''
            }
        },
        methods: {
            updateUsers() {
                this.$refs.updateUser.validate((valid) => {
                    if (valid) {
                        postRequest(
                            '/assistant/update',
                            {
                                'username': this.User.username,
                                'realname': this.User.realname,
                                "sex": this.updateUser.sex,
                                'address': this.updateUser.address,
                                'telephone': this.updateUser.telephone,
                                "age": this.updateUser.age,
                                "assStatus": this.User.assStatus
                            }
                        ).then(res => {
                            // 密码修改校验成功
                            if (res && res.flag === '1') {
                                // 保存登录信息
                                this.$store.commit('setLoginParam', res.user)
                                this.$message.success(' 护工信息修改成功');
                                // replace 不可回退  铺设可以
                                this.$router.push('/AssisHome/UpdateAssis')
                            } else {
                                this.loading = false
                                // 修改失败
                                this.$message.error(res.msg + '!');
                            }
                        })
                    } else {
                        this.$message.warning('请完整规范填写自身修改信息！');
                        return false;
                    }
                })

            },
            getLoginInfo() {
                this.User = JSON.parse(this.$store.state.loginParam)


                if (this.User.assStatus === '0') {
                    this.assStatusss = '空闲'
                }
                if (this.User.assStatus === '1') {
                    this.assStatusss = '在岗'
                }
                if (this.User.assStatus === '2') {
                    this.assStatusss = '请假'
                }
            }
        },
        mounted() {
            this.getLoginInfo()
        }
    }
</script>

<style scoped>

</style>
